package com.chinaunicom.credits.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chinaunicom.bigcustomer.model.Customer;
import com.chinaunicom.bigcustomer.service.ICustomerService;
import com.chinaunicom.bigcustomer.service.ITelinfoService;
import com.chinaunicom.credits.model.Gift;
import com.chinaunicom.credits.service.IGiftService;


@Controller
@RequestMapping("/crm/subs")
public class SubController {
	
	@Resource
	private ICustomerService customerService; 
	
	@Resource
	private ITelinfoService telinfService;
	
	@Resource
	private IGiftService giftService;

	@RequestMapping("/apply")
	public void sub_apply(HttpServletRequest request,HttpServletResponse response) throws Exception{
		request.getRequestDispatcher("/WEB-INF/credits/sub_apply.jsp").forward(request, response);
	}
	
	
	@RequestMapping("query")
	public void subquery(HttpServletRequest request,HttpServletResponse response) throws Exception{
		request.getRequestDispatcher("/WEB-INF/credits/sub_query.jsp").forward(request, response);
	}
	
	
	@RequestMapping("/manage")
	public String submanage(Model model,HttpServletRequest request) throws Exception{
		String mobile = request.getParameter("mobile");
		Customer customer = customerService.selectByPrimaryKey(mobile);
		Float telFee = telinfService.queryByMonths(mobile);
		System.out.println(telFee);
		model.addAttribute("telFee",telFee);
		model.addAttribute("customer",customer);
		return "/WEB-INF/credits/sub_manage.jsp";
		}
	
	@RequestMapping("/subapply")
	public String apply(Model model,HttpServletRequest request) throws Exception{
		String mobile = request.getParameter("mobile");
		customerService.updateApply(mobile);
		Customer customer = customerService.selectByPrimaryKey(mobile);
		Float telFee = telinfService.queryByMonths(mobile);
		model.addAttribute("telFee",telFee);
		int factor = customerService.queryFactor(mobile);
		if(telFee > 0){
			customer.setnIntf(telFee*factor);
			customerService.updateIntf(customer);
		}
		model.addAttribute("customer",customer);
		return "/WEB-INF/credits/sub_manage.jsp";
		}
	

	
	@RequestMapping("/queryByPhoneAndId")
	public String customerQuery(HttpServletRequest request,Model model,HttpSession session) throws Exception{
		String mobile = request.getParameter("mobile");
		String id = request.getParameter("Idumber");
		Customer customer = customerService.queryByPhoneAndId(mobile,id);
		if(customer != null && !"".equals(customer)){
			List<Gift> gift = giftService.queryAll();
			session.setAttribute("customer", customer);
			model.addAttribute("gift", gift);
			return "/WEB-INF/credits/sub_pay.jsp";
		}else {
			return "/WEB-INF/credits/sub_query.jsp";
		}
	}
	
	@RequestMapping("/queryByType")
	public String queryByType(Model model,HttpServletRequest request) throws Exception{
		String giftType = new String(request.getParameter("giftType").getBytes("iso-8859-1"), "utf-8");	
		model.addAttribute("giftType",giftType);
		if(("全部").equals(giftType)){
			List<Gift> gift = giftService.queryAll();
			model.addAttribute("gift",gift);
		}else {
			List<Gift> gift = giftService.queryByType(giftType);
			model.addAttribute("gift",gift);
		}
		return "/WEB-INF/credits/sub_pay.jsp";
		}
	
	@RequestMapping("/queryByName")
	public String queryByName(Model model,HttpServletRequest request) throws Exception{
		String giftName = request.getParameter("giftName");
		List<Gift> gift = giftService.queryByName(giftName);
		model.addAttribute("gift",gift);
		return "/WEB-INF/credits/sub_pay.jsp";
		}
	
	@RequestMapping("/exchange")
	public String exchange(Model model,HttpServletRequest request,HttpSession session) throws Exception{
		String mobile = request.getParameter("mobile");
		Customer customer = customerService.selectByPrimaryKey(mobile);
		List<Gift> gift = giftService.queryAll();
		session.setAttribute("customer", customer);
		model.addAttribute("gift", gift);
		return "/WEB-INF/credits/sub_pay.jsp";
		}
	
}
